package com.bahiaapk;


import com.bahiaapk.dto.TwitterMessage;

import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.auth.AccessToken;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class RetweetActivity extends Activity {

	private static final String CONSUMER_KEY 			 = "EYPN370U6tdZINgDN413mA";
	private static final String CONSUMER_SECRET 		 = "fFHrAFcyW7EAqXabeJ7QWuj3uAFfC9q6Sf2woUQ8pc";
	private static final String PREF_ACCESS_TOKEN_SECRET = "accessTokenSecret";
	private static final String PREF_ACCESS_TOKEN 		 = "accessToken";
	public static final String	CALLBACK_SCHEME			 = "Connekt";
	public static final String	CALLBACK_HOST			 = "callback";
	public static final String	CALLBACK_URL			 = "twitter://callback_conneket"  ;

	private EditText titleTX;	
	private TwitterMessage twitterMessage;
	private Twitter mTwitter;
	private  String token;
	private  String secret;

	private SharedPreferences mPrefs;			
	
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.retweet);
        titleTX = (EditText) findViewById(R.id.titleTX);
        Bundle loadInfo = getIntent().getExtras();
        twitterMessage = (TwitterMessage) loadInfo.getSerializable("twitterMessage");
        
        if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }

       
    }

	
	

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		

        // Create a new shared preference object to remember if the user has
		mPrefs = getSharedPreferences("twitterPrefs", MODE_PRIVATE);
		// Load the twitter4j helper
		mTwitter = new TwitterFactory().getInstance();
       // Tell twitter4j that we want to use it with our app
       mTwitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
       token = mPrefs.getString(PREF_ACCESS_TOKEN, null);
       secret= mPrefs.getString(PREF_ACCESS_TOKEN_SECRET, null);
       
       if (mPrefs.contains(PREF_ACCESS_TOKEN) && token!=null && secret!=null) {
           loginAuthorisedUser();
        }else{
	       	Intent intent = new Intent(this, LoginTwitterActivity.class);//ServiceSelectActivity.class);
	       	startActivity(intent);
        }
	}
	/**
     * The user had previously given our app permission to use Twitter</br>
     * Therefore we retrieve these credentials and fill out the Twitter4j helper
     */
    private void loginAuthorisedUser() {
    	
 
        // Create the twitter access token from the credentials we got previously
        AccessToken at = new AccessToken(token, secret);
       
        
        mTwitter.setOAuthAccessToken(at); 
        detail();
     }
     

    
    
    
   
 
     /**
      * Send a tweet on your timeline, with a Toast msg for success or failure
      */
     public void retweet() {
    	 try {
    		 mTwitter.retweetStatus(twitterMessage.getId());
    		 Toast.makeText(this, "Retweet Successful!", Toast.LENGTH_SHORT).show();
    	 } catch (TwitterException e) {
    		 e.printStackTrace();
    	 }
    	 this.finish();
     }     
 
     public void loginTwitter(View view){
 		Intent intent = new Intent(this, LoginTwitterActivity.class);//ServiceSelectActivity.class);
 		startActivity(intent);
 	}
 	
  
      
    private void detail() {


    	titleTX.setText("@"+twitterMessage.getName()+" "+twitterMessage.getMessage());
    	retweet();
    }

   

    public void close(View view){
    	this.finish();
    }
}
